package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Result;
import com.koron.util.secret.SecretReqUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 从集抄获取集中器信息，查询条件包括结构化地址id、编号和名称的模糊查询
 * @author 甄玉晴
 *
 */
public class ConcentratorSelect implements ServerInterface  {
	
	private static Logger logger = LogManager.getLogger(ConcentratorSelect.class);

	@SuppressWarnings("unchecked")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			Map<String,String> map=JsonUtils.objectToPojo(req.getData(),Map.class);
			List<Map> list=new ArrayList<Map>();
			//根据水司编号从集抄系统中获取集中器编号
			// list=getList(ApplicationConfig.getJcUrl(),userInfo.getCurWaterCode(),map.get("adminArea"),map.get("fuzzyQuery"));

			/*Map map1=new HashMap();
			map1.put("code", "123");
			map1.put("address", "dizhi1");
			map1.put("modelName", "地址");
			Map map2=new HashMap();
			map2.put("code", "12334");
			map2.put("address", "dizhi2");
			map2.put("modelName", "地址2");
			list.add(map1);
			list.add(map2);*/
			info.setData(list);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}
		private static List<Map> getList(String url, String group,String adminArea,String fuzzyQuery){
			 List<Map> list=new ArrayList<Map>();
		 	Map<String,Object> data=new HashMap<String,Object>();
		 	data.put("group", group);
		 	data.put("adminArea", adminArea);
		 	if(!StringUtils.isEmpty(fuzzyQuery)) {
		 		data.put("fuzzyQuery", fuzzyQuery);
		 	}
		 	com.koron.util.secret.RequestBean reqBean = new com.koron.util.secret.RequestBean();
		    reqBean.setUrl(url);
		    reqBean.setData(data);
		    String result = null;
		    try {
		        result = SecretReqUtil.sendToJc(reqBean,"getConcentrator");
		        Result resultdata= JsonUtils.jsonToPojo(result, Result.class);
		        if(resultdata.getRetCode()==0&&resultdata.getRetVal()!=null) {
		        	list=JsonUtils.objectToListIgnoreNone(resultdata.getRetVal(), Map.class);
		        }
		    }catch (Exception e){
		        logger.error("调用集抄系统失败",e);
		        return null;
		    }
		    return list;
		}

}
